#include "anim_texture.h"

namespace xge{
namespace graphics{

  void AnimTexture
    ::Action( BaseNode* node , const Timer::tick_t& diff_time ) {
      // testing if we are in pause status
      if( pause_ )
        return;
      // testing if we need to do the forwarding
      time_diff_+=diff_time;
      if( time_diff_ < fps_ )
        return;
      time_diff_ = 0;
      // testing where we are now
      if( forward() ) {
        ++frame_position_;
        if( frame_position_ = frames_+1 ) {
          if(loop())
            frame_position_ = 1;
          else
            return;
        } 
      } else {
        --frame_position_;
        if( frame_position_ == 0 ) {
          if(loop()) 
            frame_position_ = frames_;
          else
            return;
        }
      }
      // according to the current frame position to locate the index
      // our index starts from zero , however the animation starts from 1
      size_t col_idx = (frame_position_-1) / row_num_ ;
      size_t row_idx = (frame_position_-1) - (col_idx*row_num_) ;
      // getting the index to fetch the texture now
      Rect frame_rect( 
        texture_rect_.left_up_point().x() + row_idx*texture_rect_.width() ,
        texture_rect_.left_up_point().y() + col_idx*texture_rect_.height(),
        texture_rect_.width(),
        texture_rect_.height() );
      node->set_texture_rect(frame_rect);
      if( !texture_.Null() )
        node->set_texture(texture_);
  }


}// namespace graphics
}// namespace xge

